﻿var PasswordStrength = {
    watch: function (password_input_id) {
        var input = $(password_input_id);
        if (!input) {
            foodbank.common.showDebug("Password Strength input missing: " + password_input_id);
        }
        var container = $('<div></div>').addClass('password_strength_container');
        input.parent().append(container);

        var bg_elm = $('<div></div>').addClass('password_strength_bg');
        container.append(bg_elm);

        var elm = $('<div></div>');
        elm = $(elm);
        elm.cur_score = 0;
        elm.addClass("password_strength");
        container.append(elm);

        for (var i = 1; i <= 3; i++) {
            var style = 25 * i + '%';
            container.append($('<div></div>').addClass('password_strength_separator').css('left', style));
        }
        var password_desc = $('<div></div>').addClass('password_strength_desc');
        //password_desc.update('&nbsp;');
        container.append(password_desc);

        var clearfix = $('<div></div>').addClass('clearfix');

        container.append(clearfix);

        var color_map = [
            "",
            "#c81818",
            "#ffac1d",
            "#a6c060",
            "#27b30f"
        ];

        var word_map = [
        // TRANSLATORS the following strings refer to a password strength meter on the registration page
            ["", Globalize.localize("veryweakpass")],
            ["#c81818", Globalize.localize("weakpass")],
            ["#e28f00", Globalize.localize("goodpass")],
            ["#8aa050", Globalize.localize("mediumpass")],
            ["#27b30f", Globalize.localize("greatpass")]
        ];

        var last_pwd = '';

        var animator = function () {
            var pwd = input.val();
            if (pwd == last_pwd) {
                return;
            }
            last_pwd = pwd;

            var score, word;
            score = PasswordStrength.score(pwd);
            word = word_map[score];
   
            password_desc.css('color', word[0]);
            password_desc.html(pwd.length ? word[1] : "&nbsp;");

            var color_ind = score;
            elm.css('background-color', color_map[color_ind]);
            elm.cur_score = score;
            if (score == 0) {
                elm.css('width', '0%');
            }
            else {
                elm.css('width', (score * 25) + '%');
            }
        };

        setInterval(animator, 350);
    },
    get_user_inputs: function () {
        var inputs = [];
        uname = $('#username');
        fname = $('#firstname');
        lname = $('#lastname');
        email = $('#email');
        if (uname) {
            inputs.push(uname.val());
        }
        if (fname) {
            inputs.push(fname.val());
        }
        if (lname) {
            inputs.push(lname.val());
        }
        if (email) {
            inputs.push(email.val());
        }
        return inputs;
    },
    score: function (str) {
        if (!window.zxcvbn) {
            return 0;
        }
        var result = zxcvbn(str, PasswordStrength.get_user_inputs());
        return result.score;
    }
};

// inline copy of /static/javascript/external/zxcvbn-async.js
// (with modified src to match metaserver's zxcvbn.js location.)
(function () { var a; a = function () { var a, b; b = document.createElement("script"); b.src = "/Scripts/zxcvbn.js"; b.type = "text/javascript"; b.async = !0; a = document.getElementsByTagName("script")[0]; return a.parentNode.insertBefore(b, a) }; null != window.attachEvent ? window.attachEvent("onload", a) : window.addEventListener("load", a, !1); }).call(this);